import { applyMiddleware, legacy_createStore as createStore } from 'redux';
import rootReducer from '@/reducers';
import { thunk } from 'redux-thunk';
import { persistReducer, persistStore } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import { REDUX_KEY } from '@/constants/reduxConstant.js';
import TokenUtil from '@/utils/token.js';

const persistConfig = {
  key: REDUX_KEY,
  storage: storage,
  blacklist: ['tabs']
};

const persistedReducer = persistReducer(persistConfig, rootReducer);

const store = createStore(persistedReducer, applyMiddleware(thunk));

const persistor = persistStore(store);

// 清除所有数据
const clearPersistor = () => {
  TokenUtil.removeToken();
  localStorage.removeItem(`persist:${REDUX_KEY}`);
};

export { clearPersistor, persistor, store };
